package com.starblink.android.guang.btmnav

import android.content.Context
import android.graphics.Color
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.view.marginBottom
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationMenuView
import com.google.android.material.bottomnavigation.BottomNavigationView

object BottomNavigationViewHelper {

    /**
     * 设置所有图片尺寸
     *
     * @param view BottomNavigationView
     * @param width 宽
     * @param height 高
     */
    fun setAllImageSize(
        ctx: Context,
        view: BottomNavigationView,
        width: Float,
        height: Float
    ) {
        val menuView = view.getChildAt(0) as BottomNavigationMenuView
        try {
            for (i in 0 until menuView.childCount) {

                val item =
                    menuView.getChildAt(i) as BottomNavigationItemView
                val iconView =
                    item.findViewById<ImageView>(com.google.android.material.R.id.navigation_bar_item_icon_view)
                val layoutParams: ViewGroup.LayoutParams = iconView.layoutParams
                val displayMetrics: DisplayMetrics = ctx.resources.displayMetrics
                layoutParams.height =
                    TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_DIP,
                        height,
                        displayMetrics
                    ).toInt()
                layoutParams.width =
                    TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_DIP,
                        width,
                        displayMetrics
                    ).toInt()
                iconView.layoutParams = layoutParams
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 设置单个条目图片尺寸
     *
     * @param position 条目位置
     * @param view BottomNavigationView
     * @param width 宽
     * @param height 高
     */
    fun setOneImageSize(
        ctx: Context,
        position: Int,
        view: BottomNavigationView,
        width: Float,
        height: Float
    ) {
        val menuView = view.getChildAt(0) as BottomNavigationMenuView
        try {
            for (i in 0 until menuView.childCount) {
                if (i == position) {
                    val item =
                        menuView.getChildAt(i) as BottomNavigationItemView
                    item.setPadding(0, 0, 0, 0)
                    val iconView =
                        item.findViewById<ImageView>(com.google.android.material.R.id.navigation_bar_item_icon_view)
                    val layoutParams: ViewGroup.LayoutParams = iconView.layoutParams
                    val displayMetrics: DisplayMetrics = ctx.resources.displayMetrics
                    layoutParams.height =
                        TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_DIP,
                            height,
                            displayMetrics
                        ).toInt()
                    layoutParams.width =
                        TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_DIP,
                            width,
                            displayMetrics
                        ).toInt()
                    iconView.layoutParams = layoutParams
//                    imageView.layoutParams.width = width
//                    imageView.layoutParams.height = height
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

}

